//
//  LoginViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/6.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "LoginViewController.h"
#import "APIRequest.h"
#import "MyselfViewController.h"
#import "RegistViewController.h"
#import "ForgetViewController.h"
@interface LoginViewController ()
{
    UITextField *name;
    UITextField *password;
    UITextView *textView;
}
@end

@implementation LoginViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"登录";
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat width =   self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    name = [[UITextField alloc] initWithFrame:CGRectMake(width / 1.95, height / 4.9, width / 1.4, 40)];
    name.center = CGPointMake(width / 1.95, height / 4.9);
    //name.backgroundColor = [UIColor redColor];
    [name.layer setCornerRadius:4];
    [name.layer setBorderWidth:1];
    name.borderStyle = UITextBorderStyleRoundedRect;
    name.clearButtonMode = UITextFieldViewModeWhileEditing;
    name.placeholder = @"输入用户名";
    name.textColor = [UIColor grayColor];
    name.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:name];
    [name release];
    
    password = [[UITextField alloc] initWithFrame:CGRectMake(width / 1.95, height / 3.4, width / 1.4, 40)];
    password.center = CGPointMake(width / 1.95, height / 3.4);
    password.placeholder = @"输入密码";
    [password.layer setCornerRadius:4];
    [password.layer setBorderWidth:1];
    password.secureTextEntry = YES; //密码
    password.textColor = [UIColor grayColor];
    password.borderStyle = UITextBorderStyleRoundedRect;
    password.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:password];
    [password release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(width / 1.99, height / 2.25, width / 1.4, 30);
    button.center = CGPointMake(width / 1.99,  height / 2.25);
    
    button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(width / 1.50, height / 2.55, width / 1.77, 20)];
    label.center = CGPointMake(width / 1.50, height / 2.55);
    label.text = @"没有账号, 请";

    [self.view addSubview:label];
    UIButton *buttonforget = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonforget.frame = CGRectMake(width / 3.8, height / 2.55, width / 1.77, 20);
    buttonforget.center = CGPointMake(width / 3.8, height / 2.55);
    [buttonforget setTitle:@"忘记密码?" forState:UIControlStateNormal];
    buttonforget.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    [buttonforget setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [buttonforget addTarget:self action:@selector(forget:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonforget];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(width / 1.35, height / 2.55, width / 4, 20);
    button1.center = CGPointMake(width / 1.35, height / 2.55);
    button1.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    [button1 setTitle:@"注册" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
}
- (void)regist:(id)sender
{
    RegistViewController *regist = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:regist animated:YES];
    [regist release];
}
- (void)forget:(id)sender
{
    ForgetViewController *forget = [[ForgetViewController alloc] init];
    [self.navigationController pushViewController:forget animated:YES];
    [forget release];
}
- (void)login:(id)sender
{
    
    APIRequest *manager = [APIRequest sharedClient];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:name.text,@"username",password.text,@"password", nil];
    
    [manager POST:@"account/login?" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
   
        if ([[[responseObject firstObject] objectForKey:@"value"]isEqualToString:@"true"]) {
           
            MyselfViewController *myself = [[MyselfViewController alloc] init];
            myself.strusername = name.text;

            [self.navigationController pushViewController:myself animated:YES];
            name.text = myself.strusername;
            NSLog(@"%@", name.text);
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
        textView.text = [NSString stringWithFormat:@"error:%@",error];
        NSLog(@"error ===== %@",textView.text);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登录失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

    }];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [name resignFirstResponder];
    [password resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
